package com.evil.account.enums.login;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum AuthTypeEnum {

    /**
     * 登录方式
     */
    SCAN(1, "扫码"),
    EMAIL(2, "邮箱"),
    ACCOUNT(3, "账号密码"),
    CAPTCHA(4, "手机验证码");

    @Getter
    private final int type;
    @Getter
    private final String name;

    public static Optional<AuthTypeEnum> isExistByType(Integer type) {
        if (null == type) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type == type).findFirst();
    }

    public static AuthTypeEnum findByType(Integer type) {
        return AuthTypeEnum.isExistByType(type).orElseThrow(AuthTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "登录方式参数错误");
    }
}
